import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hxdj/common/base_page/bottom_navigation_bar.dart';
import 'package:hxdj/common/my_global.dart';
import 'package:hxdj/common/my_upgrade_checker.dart';
// import 'package:hxdj/common/my_upgrade_checker.dart';
import 'package:hxdj/common/routers/base_routers.dart';
import 'package:hxdj/home/address/edit_address/edit_address_viewmodel.dart';
import 'package:hxdj/home/address/select_address/select_address_viewmodel.dart';
import 'package:hxdj/home/home_page.dart';
import 'package:hxdj/home/home_viewmodel.dart';
import 'package:hxdj/home/order/order_create_viewmodel.dart';
import 'package:hxdj/login/login_page.dart';
import 'package:hxdj/login/user_agreement.dart';
import 'package:hxdj/message/message_page/message_page.dart';
import 'package:hxdj/message/message_page/message_viewmodel.dart';
import 'package:hxdj/mine/mine_page.dart';
import 'package:hxdj/recommend_project/recommend_project_page.dart';
import 'package:hxdj/recommend_project/recommend_project_viewmodel.dart';
import 'package:provider/provider.dart';





class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, designSize: const Size(375, 667));

    return Consumer(builder: (context, MyGlobalViewModel model, child) {
      return MultiProvider(
          providers: [
            
            ChangeNotifierProvider(create: (context) => HomeViewModel()), // 首页
            ChangeNotifierProvider(create: (context) => MessageViewModel()), // 注册你的数据模型
            ChangeNotifierProvider(create: (context) => RecommendProjectViewModel()), // 预约页面
            ChangeNotifierProvider(create: (context) => SelectAddressViewModel()), // 选择地址
            ChangeNotifierProvider(create: (context) => EditAddressViewModel()), // 编辑地址
            ChangeNotifierProvider(create: (context) => OrderCreateViewModel()), // 创建订单

            
            // 可以添加更多 Provider
          ],
          child: MaterialApp(
        title: 'Flutter Demo',
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF3DC694)),
            useMaterial3: true,
            iconButtonTheme: const IconButtonThemeData(
                style: ButtonStyle(overlayColor: MaterialStatePropertyAll(Colors.transparent))),
            appBarTheme: const AppBarTheme(
              elevation: 0,
              toolbarHeight: 80,
              surfaceTintColor: Colors.transparent,
              backgroundColor: Colors.transparent,
              systemOverlayStyle: SystemUiOverlayStyle(
                statusBarColor: Colors.transparent,
                statusBarIconBrightness: Brightness.dark,
                systemNavigationBarColor: Colors.transparent,
              ),
              iconTheme: IconThemeData(color: Color(0xFF212529)),
            )),
        onGenerateRoute: Routes.router.generator,
            home: model.isAgreeUserAgreement
                ? model.isLogin
                    ? const MyMainPage(title: 'Flutter Demo Home Page')
                    : const LoginPage()
                : const UserAgreementPage(),
        builder:  EasyLoading.init(),
          ));
    });
  }
}

class MyMainPage extends StatefulWidget {
  static String routeName = "/MyMainPage";

  const MyMainPage({super.key, required this.title});

  final String title;

  @override
  State<MyMainPage> createState() => _MyMainPageState();
}

class _MyMainPageState extends State<MyMainPage> with WidgetsBindingObserver {
  final List<Widget> _homePageWidgets = [
    const HomePage(),
    const RecommendProjectPage(),
    const MessagePage(),
    const MinePage(),
  ];

  List<TabModel> tabModelList = [
    TabModel("ic_tabbar_se_home", "ic_tabbar_un_home", "首页"),
    TabModel("ic_tabbar_se_reservation", "ic_tabbar_un_reservation", "预约"),
    TabModel("ic_tabbar_se_message", "ic_tabbar_un_message", "消息"),
    TabModel("ic_tabbar_se_mine", "ic_tabbar_un_mine", "我的"),
  ];

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      MyUpgradeChecker.checkForUpgrade(context);
    });
    WidgetsBinding.instance.addObserver(this);

  }

  @override
  void dispose() {
    //销毁监听生命周期
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    switch (state) {
      //切换前台时,可回调
      case AppLifecycleState.resumed:
        //检测用户是否升级
        // MyUpgradeChecker.checkForUpgrade(context);
        break;
      default:
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Consumer(
        builder: (context, MyGlobalViewModel model, child) => Scaffold(
        body: IndexedStack(
              index: model.index,
          children: _homePageWidgets,
        ),
        bottomNavigationBar: CABottomNavigationBar(
            length: _homePageWidgets.length,
                currentIndex: model.index,
            callback: (int index) {
                  model.setIndex(index);
              
            },
                tabList: tabModelList)));
    
    
  }
}
